<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="800px" @open="open" @close="close">
    <el-row v-if="fromApproval === '1'">
      <el-col :span="24">
        <el-button type="danger" style="float: right" size="small" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
      </el-col>
    </el-row>
    <br>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务名称" prop="taskname">
            <el-input v-model="model.taskname" type="text" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="更新内容" prop="taskfeedbackdetail" :rules="rules.validateRequire">
            <el-input v-model="model.taskfeedbackdetail" type="textarea" :rows="4" maxlength="800">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务进度" prop="taskschedule" :rules="rules.validateRequire">
            <el-input-number v-model="model.taskschedule" controls-position="right" :step="1" :min="0" :max="100" style="width:93%"></el-input-number><span>%</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际完成日期" prop="taskactualenddate">
            <el-date-picker v-model="model.taskactualenddate" type="date" placeholder="例：2021-01-01" value-format="yyyy-MM-dd" :disabled="isFinish">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="更新日期" prop="feedbackdate">
            <el-date-picker v-model="model.feedbackdate" type="date" placeholder="更新日期" value-format="yyyy-MM-dd"
              :disabled="true">
            </el-date-picker>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="12">
          <el-form-item label="任务状态" prop="taskstate" :rules="rules.validateRequire">
            <el-select v-model="model.taskstate" filterable clearable @change="setState()" :disabled="true">
              <el-option v-for="item in codeModel.taskstateList" :key="item.code" :label="item.name"
                :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="任务实际完成日期" prop="taskactualenddate">
            <el-date-picker v-model="model.taskactualenddate" type="date" placeholder="年-月-日" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="更新人姓名" prop="username">
            <el-input v-model="model.username" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人工号" prop="usercode">
            <el-input v-model="model.usercode" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <tl-approval-history ref="history" sourceType="GpmProjectTaskFeedback" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <!-- <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit"
        :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button> -->
      <!-- <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit">
        <svg-icon icon-class="send" />提交更新
      </el-button> -->
      <tl-attachment sourceType="GpmProjectTaskFeedback" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <!-- <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button> -->
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: {},
  data() {
    return {
      moduleTitle: '任务进度更新',
      urlPrefix: '/sd/projectTaskFeedback/',
      fromApproval: '',
      url: {
        // 额外的url
        queryCodeList: '/sd/projectTask/dataCodeList'
      },
      isFinish: false
    }
  },
  methods: {
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    },
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'feedbackdate', new Date())
        this.$set(this.model, 'taskid', this.taskid)
        this.$set(this.model, 'taskname', this.taskname)
        this.$set(this.model, 'taskschedule', this.taskschedule)
        this.$set(this.model, 'taskstate', this.taskstate)
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    /** 保存确认 */
    // doSubmitEdit() {
    //   if (!this.beforeSubmit()) {
    //     return
    //   }
    //   this.$refs.form.validate(valid => {
    //     if (valid) {
    //       if (this.isAdd) {
    //         this.doAdd()
    //         return true
    //       } else {
    //         this.doEdit()
    //       }
    //     } else {
    //       return false
    //     }
    //   })
    // },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      postAction(this.url.add + '?billuid=' + this.uid, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.model = res.result
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    /** 提交审批 */
    doSubmitApproval() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.model.taskschedule === 100 && !this.model.taskactualenddate) {
            this.$tap.alert('请填写实际完成日期！')
            return
          }
          if (this.model.taskschedule !== 100) {
            this.$set(this.model, 'taskactualenddate', '')
          }
          if (this.model.taskschedule === 100) {
            this.$set(this.model, 'taskstate', '2')
          }
          if (this.model.taskstate === '2') {
            this.$set(this.model, 'taskschedule', 100)
          }
          if (this.model.taskstate === '0') {
            this.$tap.alert('任务已开始，请选择正确的任务状态!', 'warning')
            return
          }
          if (!this.url.submit) {
            this.$tap.alert('请设置审批地址!', 'warning')
            return
          }
          if (!this.beforeApproval()) {
            return
          }
          this.submitLoading = true
          this.$tap
            .confirm('确定提交更新吗？提交后，更新内容不可修改！')
            .then(_ => {
              putAction(this.url.submit, this.model).then(res => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.afterApproval()
                  this.visible = false
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
                this.submitLoading = false
              })
            })
            .catch(_ => {
              this.submitLoading = false
            })
        }
      })
    },
    setState() {
      if (this.model.taskstate === '2') {
        this.$set(this.model, 'taskschedule', 100)
      }
    },
    /** 页面打开时执行 - zhanglongfei 2019.10.18 */
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then(_ => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        }
      })
    }
  },
  created() {
    this.doQueryCode()
  },
  /**  查询编码表*/
  doQueryCode() {
    if (this.url.queryCodeList) {
      getAction(this.url.queryCodeList, {}).then(res => {
        if (res.success) {
          this.codeModel = res.result
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    }
  }
}
</script>

